package org.example.service.impl;

import org.example.Mapper.ActivityMapper;
import org.example.Mapper.ActivityRecordMapper;
import org.example.Mapper.ApplyRecordMapper;
import org.example.Mapper.UserMapper;
import org.example.domain.Activity;
import org.example.domain.ActivityRecord;
import org.example.domain.ApplyRecord;
import org.example.domain.User;
import org.example.service.ApplyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class ApplyRecordServiceImpl implements ApplyRecordService {

    @Autowired
    private ApplyRecordMapper applyRecordMapper;

    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public void addApplyRecord(ApplyRecord applyRecord) {
        applyRecordMapper.addApplyRecord(applyRecord.getUserId(),new Date(),applyRecord.getActivityRecordIdOne()
                ,applyRecord.getActivityRecordIdTwo(),applyRecord.getActivityRecordIdThree(),
                applyRecord.getActivityRecordIdFour(),applyRecord.getActivityRecordIdFive(),
                applyRecord.getActivityRecordIdSix());
        activityRecordMapper.changeStatusToOne(applyRecord.getActivityRecordIdOne());
        activityRecordMapper.changeStatusToOne(applyRecord.getActivityRecordIdTwo());
        activityRecordMapper.changeStatusToOne(applyRecord.getActivityRecordIdThree());
        activityRecordMapper.changeStatusToOne(applyRecord.getActivityRecordIdFour());
        activityRecordMapper.changeStatusToOne(applyRecord.getActivityRecordIdFive());
        activityRecordMapper.changeStatusToOne(applyRecord.getActivityRecordIdSix());
    }

    @Override
    public ApplyRecord[] getApplyRecords() {
        ApplyRecord[] applyRecords=applyRecordMapper.getApplyRecords();
        for (ApplyRecord applyRecord : applyRecords) {
            User user= userMapper.getAllById(applyRecord.getUserId());
            applyRecord.setName(user.getName());
            ActivityRecord[] activityRecords = new ActivityRecord[6];
            ActivityRecord activityRecordOne=activityRecordMapper.getActivityRecordById(
                    applyRecord.getActivityRecordIdOne());//获取申请素拓记录中提交的六次活动记录
            ActivityRecord activityRecordTwo=activityRecordMapper.getActivityRecordById(
                    applyRecord.getActivityRecordIdTwo());//获取申请素拓记录中提交的六次活动记录
            ActivityRecord activityRecordThree=activityRecordMapper.getActivityRecordById(
                    applyRecord.getActivityRecordIdThree());//获取申请素拓记录中提交的六次活动记录
            ActivityRecord activityRecordFour=activityRecordMapper.getActivityRecordById(
                    applyRecord.getActivityRecordIdFour());//获取申请素拓记录中提交的六次活动记录
            ActivityRecord activityRecordFive=activityRecordMapper.getActivityRecordById(
                    applyRecord.getActivityRecordIdFive());//获取申请素拓记录中提交的六次活动记录
            ActivityRecord activityRecordSix=activityRecordMapper.getActivityRecordById(
                    applyRecord.getActivityRecordIdSix());//获取申请素拓记录中提交的六次活动记录
            Activity activityOne=activityMapper.getAllById(activityRecordOne.getActivityId());
            activityRecordOne.setName(activityOne.getName());
            activityRecordOne.setDate(activityOne.getDate());
            switch (activityOne.getTag()){
                case "0":
                    activityRecordOne.setTag("思政培根");
                    break;
                case "1":
                    activityRecordOne.setTag("学科启智");
                    break;
                case "2":
                    activityRecordOne.setTag("体育强魄");
                    break;
                case "3":
                    activityRecordOne.setTag("美育润心");
                    break;
                case "4":
                    activityRecordOne.setTag("劳动强基");
                    break;
                case "5":
                    activityRecordOne.setTag("创新创业");
                    break;
                case "6":
                    activityRecordOne.setTag("其他");
                    break;
                default:
                    activityRecordOne.setTag("未分类");
            }
            Activity activityTwo=activityMapper.getAllById(activityRecordTwo.getActivityId());
            activityRecordTwo.setName(activityTwo.getName());
            activityRecordTwo.setDate(activityTwo.getDate());
            switch (activityTwo.getTag()){
                case "0":
                    activityRecordTwo.setTag("思政培根");
                    break;
                case "1":
                    activityRecordTwo.setTag("学科启智");
                    break;
                case "2":
                    activityRecordTwo.setTag("体育强魄");
                    break;
                case "3":
                    activityRecordTwo.setTag("美育润心");
                    break;
                case "4":
                    activityRecordTwo.setTag("劳动强基");
                    break;
                case "5":
                    activityRecordTwo.setTag("创新创业");
                    break;
                case "6":
                    activityRecordTwo.setTag("其他");
                    break;
                default:
                    activityRecordTwo.setTag("未分类");
            }
            Activity activityThree=activityMapper.getAllById(activityRecordThree.getActivityId());
            activityRecordThree.setName(activityThree.getName());
            activityRecordThree.setDate(activityThree.getDate());
            switch (activityThree.getTag()){
                case "0":
                    activityRecordThree.setTag("思政培根");
                    break;
                case "1":
                    activityRecordThree.setTag("学科启智");
                    break;
                case "2":
                    activityRecordThree.setTag("体育强魄");
                    break;
                case "3":
                    activityRecordThree.setTag("美育润心");
                    break;
                case "4":
                    activityRecordThree.setTag("劳动强基");
                    break;
                case "5":
                    activityRecordThree.setTag("创新创业");
                    break;
                case "6":
                    activityRecordThree.setTag("其他");
                    break;
                default:
                    activityRecordThree.setTag("未分类");
            }
            Activity activityFour=activityMapper.getAllById(activityRecordFour.getActivityId());
            activityRecordFour.setName(activityFour.getName());
            activityRecordFour.setDate(activityFour.getDate());
            switch (activityFour.getTag()){
                case "0":
                    activityRecordFour.setTag("思政培根");
                    break;
                case "1":
                    activityRecordFour.setTag("学科启智");
                    break;
                case "2":
                    activityRecordFour.setTag("体育强魄");
                    break;
                case "3":
                    activityRecordFour.setTag("美育润心");
                    break;
                case "4":
                    activityRecordFour.setTag("劳动强基");
                    break;
                case "5":
                    activityRecordFour.setTag("创新创业");
                    break;
                case "6":
                    activityRecordFour.setTag("其他");
                    break;
                default:
                    activityRecordFour.setTag("未分类");
            }
            Activity activityFive=activityMapper.getAllById(activityRecordFive.getActivityId());
            activityRecordFive.setName(activityFive.getName());
            activityRecordFive.setDate(activityFive.getDate());
            switch (activityFive.getTag()){
                case "0":
                    activityRecordFive.setTag("思政培根");
                    break;
                case "1":
                    activityRecordFive.setTag("学科启智");
                    break;
                case "2":
                    activityRecordFive.setTag("体育强魄");
                    break;
                case "3":
                    activityRecordFive.setTag("美育润心");
                    break;
                case "4":
                    activityRecordFive.setTag("劳动强基");
                    break;
                case "5":
                    activityRecordFive.setTag("创新创业");
                    break;
                case "6":
                    activityRecordFive.setTag("其他");
                    break;
                default:
                    activityRecordFive.setTag("未分类");
            }
            Activity activitySix=activityMapper.getAllById(activityRecordSix.getActivityId());
            activityRecordSix.setName(activitySix.getName());
            activityRecordSix.setDate(activitySix.getDate());
            switch (activitySix.getTag()){
                case "0":
                    activityRecordSix.setTag("思政培根");
                    break;
                case "1":
                    activityRecordSix.setTag("学科启智");
                    break;
                case "2":
                    activityRecordSix.setTag("体育强魄");
                    break;
                case "3":
                    activityRecordSix.setTag("美育润心");
                    break;
                case "4":
                    activityRecordSix.setTag("劳动强基");
                    break;
                case "5":
                    activityRecordSix.setTag("创新创业");
                    break;
                case "6":
                    activityRecordSix.setTag("其他");
                    break;
                default:
                    activityRecordSix.setTag("未分类");
            }
            activityRecords[0]=activityRecordOne;
            activityRecords[1]=activityRecordTwo;
            activityRecords[2]=activityRecordThree;
            activityRecords[3]=activityRecordFour;
            activityRecords[4]=activityRecordFive;
            activityRecords[5]=activityRecordSix;
            applyRecord.setActivityRecords(activityRecords);
        }
        return applyRecords;
    }

    @Override
    public Integer accessApply(ApplyRecord applyRecord,Integer tag) {
        ApplyRecord applyRecord1=applyRecordMapper.getApplyRecordById(applyRecord.getId());//获取这条请求
        if(applyRecord1.getStatus()!=0){
            return 0;//该条记录已被其他管理员处理
        } else {
            applyRecordMapper.accessApply(applyRecord.getId());//设置该条申请通过
            userMapper.updateStByIdAndTag(applyRecord.getUserId(),tag);//为该条记录的申请者加一分指定素拓
            activityRecordMapper.changeStatusToTwo(applyRecord.getActivityRecordIdOne());//将六条申请的活动记录全部更新为
            // 申请成功
            activityRecordMapper.changeStatusToTwo(applyRecord.getActivityRecordIdTwo());
            activityRecordMapper.changeStatusToTwo(applyRecord.getActivityRecordIdThree());
            activityRecordMapper.changeStatusToTwo(applyRecord.getActivityRecordIdFour());
            activityRecordMapper.changeStatusToTwo(applyRecord.getActivityRecordIdFive());
            activityRecordMapper.changeStatusToTwo(applyRecord.getActivityRecordIdSix());
            return 1;
        }
    }

    @Override
    public Integer refuseApply(ApplyRecord applyRecord) {
        ApplyRecord applyRecord1=applyRecordMapper.getApplyRecordById(applyRecord.getId());//获取这条请求
        if(applyRecord1.getStatus()!=0){
            return 0;//该条记录已被其他管理员处理
        } else {
            applyRecordMapper.refuseApply(applyRecord.getId());//设置该条申请失败
            activityRecordMapper.changeStatusToZero(applyRecord.getActivityRecordIdOne());//将六条申请的活动记录全部更新为
            // 未申请/申请失败
            activityRecordMapper.changeStatusToZero(applyRecord.getActivityRecordIdTwo());
            activityRecordMapper.changeStatusToZero(applyRecord.getActivityRecordIdThree());
            activityRecordMapper.changeStatusToZero(applyRecord.getActivityRecordIdFour());
            activityRecordMapper.changeStatusToZero(applyRecord.getActivityRecordIdFive());
            activityRecordMapper.changeStatusToZero(applyRecord.getActivityRecordIdSix());
            return 1;
        }
    }

    @Override
    public ApplyRecord[] getApplyRecordsByUserId(Integer userId, Integer start) {
        return applyRecordMapper.getApplyRecordsByUserId(userId,start);
    }

}
